package com.example.elasticsearchdemo.module.user.controller;

import com.example.elasticsearchdemo.module.user.dao.UserRepository;
import com.example.elasticsearchdemo.module.user.entity.User;
import com.example.elasticsearchdemo.module.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author wangjianhua
 * @Description 测试控制器
 * @date 2021/7/8/008 10:54
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 简单的分页查询
     * @param pageable 分页参数
     * @return
     */
    @GetMapping("users")
    public HttpEntity users(Pageable pageable){
//    public HttpEntity users(){
//        Page<User> users = userRepository.findAll(pageable);
        Iterable<User> all = userService.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    @GetMapping("save")
    public String save(){
        User user = new User();
        user.setAddress("xxxx");
        user.setAge(20);
        user.setId(UUID.randomUUID().toString());
        user.setName("张三");
        user.setNickname("法外狂徒");
        user.setPhone("1383838438");
        userService.save(user);
        return "ok";
    }

    /**
     * 修改依然调用save方法 其实是先删除再增加 跟jpa略有不同
     * @param user
     * @return
     */
    @PostMapping("update")
    public String update(@RequestBody User user){
        userService.save(user);
        return "ok";
    }

    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") String id){
        userService.deleteById(id);
        return "ok";
    }

}
